﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.Text;

namespace UtilZ.Dotnet.Ex.Base
{
    /// <summary>
    /// Char类型转换接口
    /// </summary>
    public class CharTypeConverter : TypeConverterAbs
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public CharTypeConverter()
            : base(PrimitiveDataTypes.CharType)
        {

        }




        /// <summary>
        /// 将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <returns>转换后的值</returns>
        protected override object PrimitiveConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType)
        {
            object result;
            switch (valueType)
            {
                case PrimitiveDataTypes.CharType:
                    result = value;
                    break;
                case PrimitiveDataTypes.ByteType:
                    result = Convert.ToChar((byte)value);
                    break;
                case PrimitiveDataTypes.UInt16Type:
                    result = Convert.ToChar((UInt16)value);
                    break;
                case PrimitiveDataTypes.UInt32Type:
                    result = Convert.ToChar((UInt32)value);
                    break;
                case PrimitiveDataTypes.UInt64Type:
                    result = Convert.ToChar((UInt64)value);
                    break;
                case PrimitiveDataTypes.SByteType:
                    result = Convert.ToChar((sbyte)value);
                    break;
                case PrimitiveDataTypes.Int16Type:
                    result = Convert.ToChar((Int16)value);
                    break;
                case PrimitiveDataTypes.Int32Type:
                    result = Convert.ToChar((Int32)value);
                    break;
                case PrimitiveDataTypes.Int64Type:
                    result = Convert.ToChar((Int64)value);
                    break;
                case PrimitiveDataTypes.FloatType:
                    result = Convert.ToChar((float)value);
                    break;
                case PrimitiveDataTypes.DoubleType:
                    result = Convert.ToChar((double)value);
                    break;
                case PrimitiveDataTypes.DecimalType:
                    result = Convert.ToChar((decimal)value);
                    break;
                case PrimitiveDataTypes.StringType:
                    var str = (string)value;
                    if (str.Length == 0)
                    {
                        throw new InvalidCastException($"字符串长度为0,转换成字符失败");
                    }
                    else
                    {
                        result = str[0];
                    }
                    break;
                default:
                    result = Convert.ToChar(value);
                    break;
            }
            return result;
        }

        /// <summary>
        /// 转换数据到
        /// </summary>
        /// <param name="options">类型转换选项参数</param>
        /// <param name="value">要转换的值</param>
        /// <param name="valueType">值类型</param>
        /// <param name="result">转换后的值,存放在object中,如果转换失败为目标类型的默认值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(ITypeConverterOptions options, object value, PrimitiveDataTypes valueType, out object result)
        {
            bool parseRet = true;
            switch (valueType)
            {
                case PrimitiveDataTypes.CharType:
                    result = value;
                    break;
                case PrimitiveDataTypes.ByteType:
                    result = Convert.ToChar((byte)value);
                    break;
                case PrimitiveDataTypes.UInt16Type:
                    result = Convert.ToChar((UInt16)value);
                    break;
                case PrimitiveDataTypes.UInt32Type:
                    result = Convert.ToChar((UInt32)value);
                    break;
                case PrimitiveDataTypes.UInt64Type:
                    result = Convert.ToChar((UInt64)value);
                    break;
                case PrimitiveDataTypes.SByteType:
                    result = Convert.ToChar((sbyte)value);
                    break;
                case PrimitiveDataTypes.Int16Type:
                    result = Convert.ToChar((Int16)value);
                    break;
                case PrimitiveDataTypes.Int32Type:
                    result = Convert.ToChar((Int32)value);
                    break;
                case PrimitiveDataTypes.Int64Type:
                    result = Convert.ToChar((Int64)value);
                    break;
                case PrimitiveDataTypes.FloatType:
                    result = Convert.ToChar((float)value);
                    break;
                case PrimitiveDataTypes.DoubleType:
                    result = Convert.ToChar((double)value);
                    break;
                case PrimitiveDataTypes.DecimalType:
                    result = Convert.ToChar((decimal)value);
                    break;
                case PrimitiveDataTypes.StringType:
                    var str = (string)value;
                    if (str.Length == 0)
                    {
                        result = '\0';
                        parseRet = false;
                    }
                    else
                    {
                        result = str[0];
                    }
                    break;
                default:
                    result = Convert.ToChar(value);
                    break;
            }

            return parseRet;
        }


    }
}
